package cn.edu.cqvie.jdk8.joda;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;

import java.util.Date;

public class JodaTest2 {

    public static void main(String[] args) {
        //标准UTC时间：2014-11-04T09:22:54.876z

        System.out.println(convertDate2UTC(new Date()));

        System.out.println(convertUTC2Date("2019-01-05T13:31:52.988Z"));

        System.out.println(convertDate2LocalByDateTimeFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));
    }

    public static String convertDate2UTC(Date javaDate) {
        DateTime dateTime = new DateTime(javaDate, DateTimeZone.UTC);
        return dateTime.toString();
    }

    public static Date convertUTC2Date(String utcDate) {
        try {
            DateTime dateTime = DateTime.parse(utcDate, DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
            return dateTime.toDate();
        } catch (Throwable t) {
            return null;
        }
    }

    public static String convertDate2LocalByDateTimeFormat(Date javaDate, String dateFormat) {
        DateTime dateTime = new DateTime(javaDate);
        return dateTime.toString(dateFormat);
    }
}
